<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>客户还款添加</title>
        <% include("/common/common_css_js.html"){} %>
    </head>
    <body>
        <div style="width: 1000px;margin: 5px 0;margin-left: 5px;">
            <% if(so.hasPermission("cwAddRepayment:save")){%>
                <a class="mini-button" iconCls="icon-save" onclick="saveRepayment()">保存</a>
            <% } %>
        </div>

        <div id="repaymentForm" style="padding-bottom: 5px;" class="mini-fit">
            <span>发货人:</span>
            <div id="customerId" name="customerId" class="mini-autocomplete" style="width:250px;"  popupWidth="1000" textField="name" valueField="id"
                 url="${base}/pages/customer_manage/queryDeliveryCustomerByKey"  required="true" requiredErrorText="发货人不能为空"
                 searchField="key"
                 emptyText="发货人名称"
                 onvaluechanged="customerIdValueChange">
                <div property="columns">
                    <div header="ID" field="id" width="30"></div>
                    <div header="客户名称" field="name"></div>
                    <div header="客户简称" field="shortName"></div>
                    <div header="客户电话" field="phone"></div>
                    <div header="客户地址" field="address"></div>
                </div>
            </div>

            <input id="phone" name="phone" emptyText="发货人电话"  class="mini-textbox" required="false" requiredErrorText="发货人电话不能为空" />
            <input id="address" name="address" emptyText="发货人地址"  class="mini-textbox" style="width: 350px" required="false" requiredErrorText="发货人地址不能为空" />


            <div style="margin-top: 5px"></div>
            <span>还款方式:</span>
            <input id="repaymentWayId" name="repaymentWayId" class="mini-combobox" style="width:150px;" textField="name" valueField="id" emptyText="请选择..."
                  required="true" allowInput="false" showNullItem="false" nullItemText="请选择..." requiredErrorText="没有选择还款方式"/>

            <span>还款金额:</span>
            <input id="amount" name="amount" emptyText="还款金额"  class="mini-textbox" required="true" requiredErrorText="还款金额不能为空" />

            <div style="margin-top: 5px"></div>
            <input id="voucherFileId" name="voucherFileId" class="mini-hidden" required="true"/>
            <form id="fileUploadForm" style="display: inline">
                <span>还款凭证:</span>
                <input type="file" id="upLoadFileData" name="upLoadFileData" accept=".jpg,.jpeg,.png,.gif,.bmp" />
            </form>
            <a class="mini-button" iconCls="icon-upload" onclick="uploadFile()">上传凭证</a>
            <div style="margin-top: 5px"></div>
            <span>登记人:</span>
            <input id="registUserId" name="registUserId" value="${registUserId!}" class="mini-combobox" style="width:150px;" textField="name" valueField="id" emptyText="请选择..."
                                    required="true" allowInput="true" showNullItem="true" nullItemText="请选择..." requiredErrorText="没有选择登记人" />

            <span>还款日期:</span>
            <input id="registDate" name="registDate" class="mini-datepicker" style="width:200px;" value="${registDate!}"  nullValue="null" format="yyyy-MM-dd"  showOkButton="true" showClearButton="true"/>

            <div style="margin-top: 5px"></div>
            <span>备注:</span><textarea id="comment" name="comment" class="mini-textarea" style="width: 80%;height: 50px" emptyText="请输入还款备注"  required="false"></textarea>

        </div>

        <script type="text/javascript">
            mini.parse();

            let repaymentForm = new mini.Form("#repaymentForm");

            let customerId = mini.get("customerId");
            let repaymentWayId = mini.get("repaymentWayId");
            let amount = mini.get("amount");
            let voucherFileId = mini.get("voucherFileId");
            let registUserId = mini.get("registUserId");
            let registDate = mini.get("registDate");
            let phone = mini.get("phone");
            let address = mini.get("address");

            $(function (){
                // 还款方式
                let repaymentWayIdJsonData = commonRenderSysDicDropListByKey("sysDic","XT_DIC_REPAYMENT_WAY");
                repaymentWayId.load(repaymentWayIdJsonData);

                let registUserIdJsonData = getCacheList("sysUser");
                registUserId.load(registUserIdJsonData);
            });

            function customerIdValueChange(e){
                let item = e.selected;
                phone.setValue(item.phone);
                address.setValue(item.address);
            }

            /**
             * 上传还款凭证
             */
            function uploadFile(){
                let fileInput = $('#upLoadFileData').get(0).files[0];
                if(fileInput){
                    mini.confirm("确认上传还款凭证?", "提示", function(confirmAction){
                        if(confirmAction == "ok"){
                            //let  msgBoxId = mini.loading("正在上传校验及上传处理中，请稍后...","还款凭证");
                            ajaxFileUpload();
                        }
                    });
                }else{
                    mini.alert("没有选择要上传的航班信息数据Excel文件!");
                }
            }

            /**
             * 上传文件  413475243524771841
             */
            function ajaxFileUpload(){
                let  msgBoxId = mini.loading("正在上传校验及上传处理中，请稍后...","还款凭证");

                let formData = new FormData($("#fileUploadForm")[0]);
                $.ajax({
                    //接口地址
                    url: '${base}/pages/basic_data_manage/xt_upload_file/upload',
                    type: 'POST',
                    data: formData,
                    async: false,
                    cache: false,
                    contentType: false,
                    processData: false,
                    success: function (res) {
                        if(res.ok){
                            mini.hideMessageBox(msgBoxId);
                            voucherFileId.setValue(res.data);
                        }
                        showAlert(res.message);
                    },
                    error: function (res) {
                        mini.alert("网络异常，请重试！");
                    }
                });
            }

            /**
             * 保存还款信息
             */
            function saveRepayment(){
                repaymentForm.validate();
                if (repaymentForm.isValid() == false) {
                    return;
                }

                if(isNonNumeric(amount,"【还款金额】")){
                    return;
                }

                // 检测一下金额是不是数字，且要大于零
                let amountValue = public_toNumber(amount.getValue());
                if(amountValue <= 0){
                    showAlert("【还款金额】不能等于或小于零,请重新填写!",function (){
                        return;
                    });
                }


                // 检测是否已经上传凭证了
                let voucherFileIdValue = voucherFileId.getValue();
                if(public_isEmpty(voucherFileIdValue)){
                    showAlert("【还款凭证】没有上传!",function (){
                        return;
                    });
                    return;
                }

                //提交数据
                let repaymentPojo = repaymentForm.getData(true);


                let addRepaymentUrl ="${base}/pages/finance_manage/repayment/addRepayment";

                let ret = public_getServerData(addRepaymentUrl,repaymentPojo);

                if(ret.ok){
                    showAlert(ret.message,function (){
                        // 刷新当前页面
                        window.location.reload();
                    });
                }
                else{
                    showAlert(ret.message,null);
                }

            }


        </script>
    </body>
</html>